package com.hqyj.javaSpringBoot.modules.test.controller;

import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.Student;
import com.hqyj.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;


    /**
     * localhost/api/student
     * {"name":"jhs","eamil":"jhs@qq.com","age":20}
     * {"name":"jhs1","eamil":"jhs1@qq.com","age":21,"studentCard":{"cardNo":"jhs_001"}}
     */
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * localhost/api/student
     * {"id":1,"name":"jhs","eamil":"233@qq.com","age":18}
     */
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * localhost/api/student/5
     */
    @DeleteMapping("/student/{Id}")
    public Result<Object> deleteStudent(@PathVariable int Id) {
        return studentService.deleteStudent(Id);
    }

    /**
     * localhost/api/student/11
     */

    @GetMapping("/student/{Id}")
    public Student getStudentById(@PathVariable int Id) {
        return studentService.getStudentById(Id);
    }

    /**
     * localhsot/api/students
     */
    @GetMapping("/students")
    public List<Student> getStudents() {
        return studentService.getStudents();
    }

    /**
     * localhost/api/student?name=jhs2&email=jhs2@qq.com
     */
    @GetMapping("/student")
    public Student findFirstByNameAndEmail(@RequestParam String name,
                                           @RequestParam String email) {
        return studentService.findFirstByNameAndEmail(name, email);
    }

    @GetMapping("/studentAge")
    public Student findFirstByAge(@RequestParam int age) {
        return studentService.findFirstByAge(age);
    }

    @PostMapping(value = "/students", consumes = "application/json")
    public Page<Student> getStudentBySearchBean(@RequestBody SearchBean searchBean) {
        return studentService.getStudentsBySearchBean(searchBean);
    }
}
